const db = wx.cloud.database()
const _ = db.command
const app = getApp()

Component({
  properties: {
    requestId: String, //请求者的id
    activeIndex: Number
  },
  data: {
    requestUserInfo: {}
  },
  methods: {
    // 删除按钮绑定事件
    handleDelRequest() {
      let activeIndex = this.data.activeIndex
      if (activeIndex == 0) {
        wx.showModal({
          title: '提示信息',
          content: '删除好友',
          confirmText: '删除',
          success: res => {
            if (res.confirm) {
              this.removeFriend()
            }
          }
        })
      }
      else if(activeIndex == 1){
        wx.showModal({
          title: '提示信息',
          content: '删除好友请求',
          confirmText: '删除',
          success: res => {
            if (res.confirm) {
              this.removeRequest()
            }
          }
        })
      }
    },
    // 好友请求-同意绑定事件
    handleAddFriend() {
      if (this.data.activeIndex == 1) {
        wx.showModal({
          title: '提示信息',
          content: '申请好友',
          confirmText: '同意',
          success: res => {
            if(res.confirm) {
              // 更新自己。
              db.collection('pk_user').doc(app.globalData.userInfo._id).update({
                data: {
                  friendList: _.unshift(this.data.requestId)
                }
              }).then(res => {})
              // 更新他人。不能越过权限直接改别的用户的数据，要使用云函数
              wx.cloud.callFunction({
                name: 'update_request',
                data: {
                  collection: 'pk_user',
                  doc: this.data.requestId,
                  data: `{friendList: _.unshift('${app.globalData.userInfo._id}')}`
                }
              }).then(res => {})
              this.removeRequest()
              
              // 实时增加好友列表;同意后实时显示在我的好友中
              db.collection('pk_user').doc(app.globalData.userInfo._id).get().then(res => {
                let friendList = res.data.friendList
                console.log(friendList)
                getApp().globalData.userInfo.friendList = friendList
                // this.triggerEvent('delfriend', friendList)
              })
            }else if(res.cancel){
              console.log('用户点击了取消')
            }
          }
        })
      }
    },
    // 删除好友请求的显示
    removeRequest() {
      db.collection('request').where({
        userId: app.globalData.userInfo._id
      }).get().then(res => {
        let list = res.data[0].list;
        list = list.filter((val, i) => { //筛选本次删除的请求者
          return val != this.data.requestId
        })

        // 删除请求
        wx.cloud.callFunction({
          name: 'update_request',
          data: {
            collection: 'request',
            where: {
              userId: app.globalData.userInfo._id
            },
            data: { list }
          }
        }).then(res => {
           this.triggerEvent('delrequest', list)
        })
      })
    },
    // 删除好友的显示
    removeFriend() {
      // 更新自己。小程序不能删除指定下标的数组元素，只能删除头尾。所以采用重写数组的方法
      db.collection('pk_user').doc(app.globalData.userInfo._id).get().then(res => {
        console.log(res)
        let friendList = res.data.friendList;
        friendList = friendList.filter((val, i) => { //筛选本次删除的请求者
          return val != this.data.requestId
        })
        wx.cloud.callFunction({
          name: 'update_request',
          data: {
            collection: 'pk_user',
            doc: app.globalData.userInfo._id,
            data: {friendList}
          }
        }).then(res => {
           this.triggerEvent('delfriend', friendList)
        })
      })

      // 更新他人。
      db.collection('pk_user').doc(this.data.requestId).get().then(res => {
        console.log(res)
        let friendList = res.data.friendList;
        friendList = friendList.filter((val, i) => { //筛选本次删除的请求者
          return val != app.globalData.userInfo._id
        })
        // 不能越过权限直接改别的用户的数据，要使用云函数
        wx.cloud.callFunction({
          name: 'update_request',
          data: {
            collection: 'pk_user',
            doc: this.data.requestId,
            data: {friendList}
          }
        }).then(res => {})
      })

    }
  },
  lifetimes: {
    // 列表实时显示
    attached() {
      db.collection('pk_user')
        .doc(this.data.requestId)
        .field({
          avatarUrl: true,
          nickName: true
        })
       .get().then(res => {
         console.log('attached', res)
        this.setData({
          requestUserInfo: res.data
        })
      })
    }
  }
})
